<template>
    <div>
        <headers />
        <div class="container">
            <div>
                <div class="userCenterWarp">
                    <div class="userCenterTitle">个人中心</div>
                    <el-row :gutter="30">
                        <el-col :span="4">
                            <sideNav />
                        </el-col>
                        <el-col :span="20" class="mt-30">
                            <div style="display: flex;justify-content: space-between;align-items: flex-start;">
                                <el-breadcrumb separator-class="el-icon-arrow-right" class="user-breadcrumb">
                                    <el-breadcrumb-item><strong>我的地址</strong></el-breadcrumb-item>
                                </el-breadcrumb>
                                <el-button size="mini" @click="$refs.addrDialogRef.showDialog('add', {})">添加地址</el-button>
                            </div>
                            <el-table border :data="tableData" style="width: 100%">
                                <el-table-column prop="realname" label="收货人" width="160">
                                    <template slot-scope="scope">
                                        <div>
                                            {{scope.row.realname}}
                                            <el-tag size="mini" v-if="scope.row.is_default">默认地址</el-tag>
                                        </div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="mobile" label="电话/手机" width="120">
                                </el-table-column>
                                <el-table-column prop="hits" label="所在地区" min-width="100">
                                    <template slot-scope="scope">
                                        <div>{{scope.row.province}} {{scope.row.city}} {{scope.row.area}}</div>
                                    </template>
                                </el-table-column>

                                <el-table-column prop="address" label="详细地址" min-width="180">
                                </el-table-column>

                                <el-table-column fixed="right" label="操作" width="140" align="center">
                                    <template slot-scope="scope">
                                        <el-button type="text" @click="$refs.addrDialogRef.showDialog('view', scope.row)">查看</el-button>
                                        <el-button type="text" @click="$refs.addrDialogRef.showDialog('edit', scope.row)">修改</el-button>
                                        <el-button type="text" @click="handleDel(scope.row)">删除</el-button>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </el-col>
                    </el-row>
                </div>
            </div>
        </div>
        <!-- 添加地址弹窗 -->
        <addrDialog ref="addrDialogRef" @refresh="getList" />
    </div>
</template>

<script>
import headers from '@/components/header.vue'
import sideNav from './components/sideNav.vue'
import addrDialog from '../products/components/addrDialog.vue'
import { getAddressList, addressDel } from '@/api/apis'
export default {
    name: 'profile',
    components: {
        headers,
        sideNav,
        addrDialog
    },
    data () {
        return {
            currentPage: 1,
            tableData: []
        }
    },
    created () {
        this.getList()
    },
    methods: {
        getList(){
            getAddressList().then(res => {
                if (res.code === 1) {
                    this.tableData = res.data
                }
            })
        },
        handleDel (row) {
            this.$confirm('确认要删除该地址吗？').then(_ => {
                addressDel(row.id).then(res => {
                    if (res.code === 1) {
                        this.$message.success(res.msg)
                        this.tableData.splice(this.tableData.indexOf(row), 1)
                    } else {
                        this.$message.error(res.msg)
                    }
                })
            })
        }
    },
}
</script>

<style scoped lang="less">
.header {
    font-size: 18px;
    color: #333;
    font-weight: 600;
    margin-bottom: 30px;
}

/deep/.el-table__header {
    th.el-table__cell {
        background-color: #FBFBFB;
    }

}
</style>